<template>
  <div class='rockmans-show'>
    <div class='headline'>
      <span></span>牛人秀
      <a href='javascript:;' class='more' @click.stop.prevent = "toGreatMan">查看更多&gt</a>
    </div>
    <div class='row list'>
      <div class='col-xs-4 no-padding' v-for='item in manList'>
        <div class='entry'>
          <div class='up' @click.stop.prevent="toOtherDetail(item)">
            <img :src='item.userHead' class='user-head center-block' style='height: 50px'>
            <div class='nickname text-center'>{{item.nickname == '' || typeof(item.nickname) == 'undefined' ? '&nbsp;' :item.nickname}}
            </div>
            <img src='~assets/images/kapianzhong.png' class='divider'>
          </div>

          <div class='down'>
            <div class=''>
              <div class='total-yield-rate' :class='item.totalRate.indexOf("-") >= 0 ? "negative": "positive"'>
                {{item.totalRate}}
              </div>
              <div class='features'>{{item.userTag == '' || typeof(item.userTag) == 'undefined' ? '&nbsp;' :
                item.userTag}}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class='no-data' v-if='manList.length == 0'>{{error || '暂无数据'}}</div>
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import _ from 'underscore'

  export default {
    data () {
      return {
        manList: [],
        error: '数据加载中...'
      }
    },
    created: function () {
      this.fetchData()
    },
    methods: {
      fetchData () {
        let self = this
        // 牛人秀查询
        RemoteDAO.callJCL30010(function (response) {
          if (Number(response.error) === 0) {
            let list = []
            let userCodes = []
            let data = response.data.body.data
            if (data.length > 0) {
              data = _.sortBy(data, 'total_rate').reverse(); // 对数字排序，非字符串
            }
            _.each(data, function (item, index) {
              let object = {}
              object.clientId = item.client_id // 牛人代码
              object.totalRate = CommonUtil.toFixed(item.total_rate, 2) + '%'
              object.userHead = CommonUtil.getDefaultUserHead(item.client_id) // 设置头像默认值
              // 只取前三个
              if (index <= 2) {
                userCodes.push(item.client_id)
                list.push(object)
              } else {
                return false
              }
            })
            // 批量获取头像，用户标签
            RemoteDAO.callUC12008316("", userCodes.join(','), function (resp) {
              if (Number(resp.error) === 0) {
                let defaultUserTags = []
                defaultUserTags.push('短线投手，稳健型')
                defaultUserTags.push('中长玩家，激进型')
                defaultUserTags.push('均衡配置，保守型')
                _.each(resp.data.body, function (item, index) {
                  let userCode = item.USER_CODE
                  let userHead = item.USER_HEAD == '' ? CommonUtil.getDefaultUserHead(userCode) : item.USER_HEAD
                  let userTag = item.USER_TAG
                  let nickname = item.NICKNAME
                  _.each(list, function (itemTemp, indexTemp) {
                    if (itemTemp.clientId == userCode) {
                      itemTemp.userHead = userHead
                      itemTemp.nickname = nickname
                      if (userTag == '') {
                        try {
                          itemTemp.userTag = defaultUserTags[indexTemp]
                        } catch (error) {
                        }
                      } else {
                        itemTemp.userTag = userTag
                      }
                    }
                  })
                })
                self.manList = list
                self.error = ''
              } else {
                self.error = resp.data
              }
            })
          } else {
            self.error = response.data
          }
        })
      },
      /**
       * 跳转他人详情页
       * @param item
       */
      toOtherDetail (item){
        this.$router.push({ path: '/otherdetail', query: { userCode:item.clientId}});
        var kv = {"usercode": "","k2":""};
        TDAPP.onEvent("14009","",kv);
        TDAPP.send();
      },
      toGreatMan(){ //更多牛人
        this.$router.replace({path:'/greatman/bestmanbox'});
        var kv = {"usercode": "","k2":""};
        TDAPP.onEvent("14006","",kv);
        TDAPP.send();
      }
    }
  }
</script>

<style scoped>

</style>
